package com.glory.dgcpcpda.callback;

import androidx.fragment.app.FragmentManager;

import com.glory.dgcpcpda.customView.CustomLoadingDialog;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_LoadingDialog;

import java.lang.reflect.Type;

/**
 * FileName: CustomDialogEntityCallBack
 * Author: XJH
 * Date: 2020/9/21 10:42
 * Description:
 * History:
 */
public abstract class CustomDialogEntityCallBack<T> extends FreeUI_EntityCallBack<T> {
    private CustomLoadingDialog dialog = new CustomLoadingDialog();
    private FragmentManager fragmentManager;

    public CustomDialogEntityCallBack(Type type, FragmentManager fragmentManager, Object okgoTag) {
        super(type);
        this.dialog.setData(okgoTag);
        this.fragmentManager = fragmentManager;
    }

    public CustomDialogEntityCallBack(String tips, Type type, FragmentManager fragmentManager, Object okgoTag) {
        super(type);
        this.dialog.setData(okgoTag, tips);
        this.fragmentManager = fragmentManager;
    }

    public void onStart(Request request) {
        if (this.dialog != null) {
            this.dialog.show(this.fragmentManager, "loading");
        }

    }

    public void onSuccess(Response<T> response) {
        if (this.dialog != null && !this.dialog.isHidden()) {
            this.dialog.dismissAllowingStateLoss();
        }

    }

    public void onCacheSuccess(Response<T> response) {
        super.onCacheSuccess(response);
        if (this.dialog != null && !this.dialog.isHidden()) {
            this.dialog.dismissAllowingStateLoss();
        }

    }

    public void onError(Response<T> response) {
        super.onError(response);
        if (this.dialog != null && !this.dialog.isHidden()) {
            this.dialog.dismissAllowingStateLoss();
        }

    }

    public void onFinish() {
        if (this.dialog != null && !this.dialog.isHidden()) {
            this.dialog.dismissAllowingStateLoss();
        }

    }
}
